package com.hundredapp.oa.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaMakeup;
import com.hundredapp.oa.domain.OaStudent;
import com.hundredapp.oa.repository.ClassmngManager;
import com.hundredapp.oa.repository.MakeupManager;
import com.hundredapp.oa.repository.StudentManager;
import com.hundredapp.oa.util.JSEscape;
import com.hundredapp.oa.util.WebUtil;
import com.hundredapp.oa.vo.MakeupVO;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Makeup extends ActionSupport {

	private List<MakeupVO> root;
	
	private Object success;
	private String msg;
	private int id;
	private int start;
	private int limit;
	private int total;
	private String searchstudent;
	private MakeupVO makeupvo;
	private OaMakeup makeup;
	
	@Autowired
	private MakeupManager makeupmanager;
	@Autowired
	private ClassmngManager classmanager;
	@Autowired
	private StudentManager studentmanager;
	
	public String add() {
		try{
			makeup.setCreateUserId(WebUtil.GetLoginUserId());
			makeupmanager.add(makeup);
			success=true;
			msg="添加成功";
		}catch(Exception e) {
			e.printStackTrace();
			success=false;
			msg="添加失败";
		}
		return SUCCESS;
	}
	
	public String all() {
		String condition = SelectType.SELECT_ALL;
		if(searchstudent != null) {
			searchstudent = JSEscape.unescape(searchstudent);
			String stuids = "";
			List<OaStudent> stus = studentmanager.findAll(SelectType.SELECT_ALL+" and displayName like '%" +searchstudent+"%' ");
			for(int i=0; i<stus.size(); i++) {
				if(i>0)
					stuids+=",";
				stuids+=stus.get(i).getId().toString();
			}
			condition+=" and studentId in ("+stuids+")";
		}
		root=new ArrayList<MakeupVO>();
		for(OaMakeup om:makeupmanager.findAll(condition)) {
			MakeupVO temp=new MakeupVO();
			temp.setId(om.getId());
			temp.setClassName(classmanager.findById(om.getClassId()).getClassName());
			temp.setMakeupTime(om.getMakeupTime());
			temp.setStudentName(studentmanager.findById(om.getStudentId()).getDisplayName());
			temp.setWeek(om.getWeek());
			root.add(temp);
		}
		return SUCCESS;
	}
	
	public String find() {
		makeup=makeupmanager.findMakeupById(id);
		makeupvo=new MakeupVO();
		makeupvo.setId(makeup.getId());
		makeupvo.setClassName(classmanager.findById(makeup.getClassId()).getClassName());
		makeupvo.setMakeupTime(makeup.getMakeupTime());
		makeupvo.setStudentName(studentmanager.findById(makeup.getStudentId()).getDisplayName());
		makeupvo.setWeek(makeup.getWeek());
		return SUCCESS;
	}
	
	public String update() {
		try{
			makeup.setId(id);
			System.out.println(makeup.getClassId()+"--"+makeup.getCreateUserId()+"--"+makeup.getStudentId()+"--"+makeup.getWeek()+"--"+makeup.getCreateTime()+"--"+makeup.getMakeupTime());
			makeupmanager.update(makeup);
			success=true;
			msg="修改成功";
		}catch(Exception e){
			e.printStackTrace();
			success=false;
			msg="修改失败";
		}
		return SUCCESS;
	}
	
	public List<MakeupVO> getRoot() {
		return root;
	}
	public void setRoot(List<MakeupVO> root) {
		this.root = root;
	}
	public Object getSuccess() {
		return success;
	}
	public void setSuccess(Object success) {
		this.success = success;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public MakeupVO getMakeupvo() {
		return makeupvo;
	}
	public void setMakeupvo(MakeupVO makeupvo) {
		this.makeupvo = makeupvo;
	}

	public OaMakeup getMakeup() {
		return makeup;
	}

	public void setMakeup(OaMakeup makeup) {
		this.makeup = makeup;
	}

	public String getSearchstudent() {
		return searchstudent;
	}

	public void setSearchstudent(String searchstudent) {
		this.searchstudent = searchstudent;
	}
}
